{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 截断 & 考虑用户频率\n",
    "\n",
    "此代码目对应解决方案中对于similarity的改进\n",
    "\n",
    "This code is related to the improvement for similarity metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import time\n",
    "from scipy.sparse import *\n",
    "import os\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 统计每个商品的打分次数（用train）\n",
    "f = open('hot_items_map.txt', 'r')\n",
    "rating_times_map = eval(f.read())\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "load file: 6 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 7 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 7 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 7 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 7 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n",
      "load file: 8 sec\n",
      "This batch is finished, time cost: 1 sec\n"
     ]
    }
   ],
   "source": [
    "item_dict = {}\n",
    "\n",
    "for name in os.listdir('common_dense_valued_small/'):\n",
    "    start_time = time.time()\n",
    "    f = open('common_dense_valued_small/' + name, 'r')\n",
    "    l = f.read()\n",
    "    l = eval(l)\n",
    "    f.close()\n",
    "    end_time = time.time()\n",
    "    print('load file: %d sec'%((end_time - start_time)))    \n",
    "    \n",
    "    name = re.findall(r'\\d+', name)\n",
    "    start = int(name[0])\n",
    "    end = int(name[1])\n",
    "\n",
    "    \n",
    "    start_time = time.time()\n",
    "    \n",
    "    \n",
    "    for i in range(start, end):\n",
    "        tmp_list = []\n",
    "        [tmp_list.append( (x[0], round(x[1] / rating_times_map[i], 4) ) ) for x in l[i - start] if x[0] != i]\n",
    "        if len(tmp_list) > 0:\n",
    "            item_dict[i] = sorted(tmp_list,key=lambda x:x[1], reverse=True)[:500]\n",
    "            \n",
    "    end_time = time.time()\n",
    "    print('This batch is finished, time cost: %d sec'%((end_time - start_time)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "440137"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(item_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('item_Apriori.txt','w')\n",
    "f.write(str(item_dict))\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
